<template>
  <div class="margin_30">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-button type="primary" size="mini" @click="goAddAdv(0)">新增</el-button>
      </div>
      <el-table
        v-loading="loading"
        :data="list"
        border
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
      >
        <el-table-column
          label="序号"
          align="center"
          width="50"
          type="index"
          fixed="left"
        />
        <!-- prop需要展示的数据部分 -->
        <!-- label表头的名称 -->
        <el-table-column
          prop="pic"
          label="广告图片"
          align="center"
          width="170"
        >
          <template v-slot="scope">
            <img style="width:100px;height:100px" :src="scope.row.pic" alt="">
          </template>
        </el-table-column>

        <el-table-column
          prop=""
          label="时间"
          width="230"
          align="center"
        >
          <template slot-scope="scope">
            <div>开始时间：{{ scope.row.createTime }}</div>
            <div>结束时间：{{ scope.row.endTime }}</div>
          </template>
        </el-table-column>
        <el-table-column
          prop="name"
          label="广告名称"
          align="center"
          width="150"
        />
        <el-table-column
          prop="clickCount"
          label="点击数"
          align="center"
          width="120"
        />
        <el-table-column
          prop="orderCount"
          label="下单数"
          width="120"
          align="center"
        />

        <el-table-column
          label="轮播位置"
          width="120"
          align="center"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.type == 1">1</span>
            <span v-else>app首页轮播</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="url"
          label="链接广告"
          align="center"
          width="120"
        />
        <el-table-column
          prop="note"
          label="备注"
          align="center"
          width="120"
        />
        <el-table-column
          label="上下线状态"
          align="center"
          width="120"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.status == 1">上线</span>
            <span v-else>下线</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="200">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="goEdit(scope.row,1)">编辑</el-button>
            <el-button style="color:red;" size="mini" type="text" @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 编辑/新增 广告弹框 -->
    <el-dialog title="广告详情" :visible.sync="isShowDialog" width="65%" size="mini" @close="handleDialog">
      <el-form :model="form" :rules="rules" label-width="100px" size="mini">
        <el-row>
          <el-col :span="12">
            <el-form-item label="广告名称" style="width:85%">
              <el-input v-model="form.name" placeholder="广告名称" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" style="width:85%">
              <el-input v-model="form.note" placeholder="备注" autocomplete="off" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="排序" style="width:85%">
              <el-input v-model="form.sort" placeholder="排序" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品数量">
              <el-input v-model="form.clickCount" style="width:85%" placeholder="产品数量" autocomplete="off" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="链接" style="width:85%">
              <el-input v-model="form.url" placeholder="链接" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="首页轮播位置" style="width:85%">
              <el-select v-model="form.type" placeholder="订单状态">
                <el-option label="APP首页轮播" value="0" />
                <el-option label="1" value="1" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开始时间" style="width:85%">
              <el-date-picker
                v-model="form.createTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="开始时间"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束时间" style="width:85%">
              <el-date-picker
                v-model="form.endTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="结束时间"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="上下线状态" style="width:85%">
              <el-switch
                v-model="form.status"
                :active-value="true"
                :inactive-value="false"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="图片" style="width:85%">
              <el-upload
                class="upload-demo"
                :action="uploadFileOss"
                :headers="token"
                :show-file-list="false"
                :on-success="uploadImg"
              >
                <img v-if="form.pic" style="width:100px;height:100px" :src="form.pic" alt="">
                <i v-else class="el-icon-plus" style="font-size:28px;width:150px;height:150px;padding-top:50px" />
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleDialog">取 消</el-button>
        <el-button type="primary" @click="addAdv">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { adsList, addAds, updateAds, delAds } from '@/api/market/adv'
import mix from '@/mixins'
export default {
  mixins: [mix],

  data() {
    return {
      // 是否显示加载中
      loading: false,
      isShowDialog: false,
      list: [],
      rules: {},
      form: {
        'clickCount': 0,
        'createTime': '',
        'endTime': '',
        'id': '',
        'modifyTime': '',
        'name': '',
        'note': '',
        'orderCount': 0,
        'pic': '',
        'productId': '',
        'sort': 0,
        'startTime': '',
        'status': 0,
        'type': 0,
        'url': ''
      },
      state: 0
    }
  },

  created() {
    this.getAdvList()
  },

  mounted() {

  },

  methods: {
    // 上传图片
    uploadImg(e) {
      console.log(e)
      this.form.pic = e.data.fileUrl
    },
    // 获取广告列表
    getAdvList() {
      adsList()
        .then(res => {
          console.log(res)
          this.list = res.data.items
          // res.data.items.forEach(ele => {
          //   if (ele.status) {
          //     ele.status = '上线'
          //   } else {
          //     ele.status = '下线'
          //   }
          //   if (ele.type) {
          //     ele.type = '1'
          //   } else {
          //     ele.type = 'APP首页轮播'
          //   }
          // })
        })
    },
    // 编辑广告
    goEdit(val, state) {
      if (val.status === 1) {
        val.status = true
      } else {
        val.status = false
      }
      this.state = state
      this.isShowDialog = true
      this.form = Object.assign({ }, val)
    },
    // 删除广告
    del(id) {
      delAds(id)
        .then(res => {
          this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
            center: true
          }).then(() => {
            this.isShowDialog = false
            this.getAdvList()
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          })
        })
    },

    // 新增按钮 弹框出现
    goAddAdv(state) {
      this.state = state
      this.isShowDialog = true
    },
    // 取消新增
    handleDialog() {
      this.isShowDialog = false
      this.form = {}
    },
    // 确定新增广告
    addAdv() {
      this.form.status = this.form.status ? 1 : 0
      if (this.state) {
        // 修改广告
        updateAds(this.form)
          .then(res => {
            this.$confirm('是否确认修改广告?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning',
              center: true
            }).then(() => {
              this.$message({
                type: 'success',
                message: '修改成功!'
              })
            }).catch(() => {
              this.$message({
                type: 'info',
                message: '已取消修改'
              })
            })
            // 成功后关闭弹窗
            this.isShowDialog = false
            // 重新获取广告列表
            this.getAdvList()
          })
      } else {
        // 新增广告
        addAds(this.form)
          .then(res => {
            console.log(res)
            this.isShowDialog = false
            this.getAdvList()
          })
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
